import numpy as np
import matplotlib.pyplot as plt 


with open("./build/dump.dat", "rb") as fid:
    tmp = fid.read()

buf1 = np.frombuffer(tmp, dtype=np.single, count=-1)
buf2 = np.reshape(buf1, newshape=(-1,640))
print(buf2.shape)

plt.figure()
plt.subplot(121)
plt.pcolormesh(np.arange(300,340), np.arange(0,buf2.shape[0]), abs(buf2[:,300:340]))

ax = plt.subplot(122)

with open("dump_mic.pcm", "rb") as fid:
    tmp = fid.read()
mic = np.frombuffer(tmp, dtype=np.short, count=-1)
mic1 = mic[0::2]

b1 = 320
Ns = mic1.size//b1 -1
Sxx = np.zeros((Ns, b1+1), dtype=complex)

for i in range(Ns):
    block = mic1[i*b1: i*b1+640].astype(np.single)
    xf = np.fft.rfft(block)
    Sxx[i,:] = xf

plt.pcolormesh(np.log10(abs(Sxx)+0.001))

plt.show()

# with open("man_woman_16k.pcm", "rb") as fid:
#     tmp = fid.read()

# wav = np.frombuffer(tmp, dtype=np.short, count=-1)

# x1 = np.hstack((np.zeros((10,), dtype=np.short), wav, np.zeros((10,), dtype=np.short)))
# x2 = np.hstack((np.zeros((15,), dtype=np.short), wav, np.zeros((5,), dtype=np.short)))

# with open("audio0.pcm", "wb") as fid:
#     fid.write(x1.tobytes())

# with open("audio1.pcm", "wb") as fid:
#     fid.write(x2.tobytes())

# plt.figure()
# plt.plot(wav)
# plt.show()

